package com.ansion.basedb.dao.impl;


import android.util.Log;

import com.ansion.basedb.dao.ContactsDao;
import com.ansion.basedb.dao.helper.SyncContactHelper;
import com.ansion.basedb.dao.model.ContactsModel;
import com.ansion.basedb.database.IDatabaseManager;
import com.ansion.basedb.notify.UserChangeNotifier;
import com.ansion.basedb.storage.StorageSdk;
import com.ansion.basedb.util.UserBroadcastUtils;
import com.ansion.log.VLog;

import java.util.List;

public abstract class ContactsDaoImpl implements ContactsDao {

	@Override
	public void clearCache() {

	}

	@Override
	public ContactsModel getContact(long uid) {
		return getContactInner(uid);
	}

	protected ContactsModel getContactInner(long uid) {
		IDatabaseManager mainDB = StorageSdk.getMainDB();
		if (null == mainDB) {
			return null;
		}
		String selection = ContactsModel.kColumnName_UserId + "=?";
		String[] selectionArgs = new String[] { "" + uid };
		List<ContactsModel> userList = mainDB.select(ContactsModel.class, null,
				selection, selectionArgs, null, null, null, null);
		if (userList == null || userList.size() < 1) {
			return null;
		}
		return userList.get(0);
	}


	@Override
	public List<ContactsModel> loadAll(boolean fromDb) {
		IDatabaseManager mainDB = StorageSdk.getMainDB();
		if (null == mainDB) {
			return null;
		}
		String selection = null;
		String[] selectionArgs = null;
		return mainDB.select(ContactsModel.class, null, selection,
				selectionArgs, null, null, null, null);
	}

	@Override
	public void saveContact(ContactsModel contact) {
		IDatabaseManager mainDB = StorageSdk.getMainDB();
		if (null == mainDB) {
			return;
		}
		mainDB.replace(ContactsModel.class, contact);
		UserBroadcastUtils.sendContactChangeIntent(UserBroadcastUtils.kDAOCategory_RowReplace);
		UserChangeNotifier.getInstance().notifyContactAdd(String.valueOf(contact.getUserId()));
	}

	@Override
	public void saveContact(List<ContactsModel> contacts) {
		VLog.d("ANSION","saveContact list:%s", Log.getStackTraceString(new Throwable()));
		IDatabaseManager mainDB = StorageSdk.getMainDB();
		if (null == mainDB) {
			return;
		}
		mainDB.replace(ContactsModel.class, contacts);
        UserBroadcastUtils.sendContactChangeIntent(UserBroadcastUtils.kDAOCategory_RowReplace);
		for (ContactsModel contactsModel : contacts) {
			UserChangeNotifier.getInstance().notifyContactAdd(String.valueOf(contactsModel.getUserId()));
		}
	}

	@Override
	public void deleteContact(long uid) {
		IDatabaseManager mainDB = StorageSdk.getMainDB();
		if (null == mainDB) {
			return;
		}
		String selection = ContactsModel.kColumnName_UserId + "=?";
		String[] selectionArgs = new String[] { "" + uid };
		mainDB.delete(ContactsModel.class, selection, selectionArgs);
        UserBroadcastUtils.sendContactChangeIntent(UserBroadcastUtils.kDAOCategory_RowRemove);
		UserChangeNotifier.getInstance().notifyContactDelete(String.valueOf(uid));
		SyncContactHelper.deleteContactFromPhoneBook(String.valueOf(uid));
	}

	@Override
	public void deleteContacts(List<Long> uids) {
		IDatabaseManager mainDB = StorageSdk.getMainDB();
		if (null == mainDB) {
			return;
		}
		String selection = ContactsModel.kColumnName_UserId + " in ( "+getDeleteList(uids)+" )";
		mainDB.delete(ContactsModel.class, selection, null);
        UserBroadcastUtils.sendContactChangeIntent(UserBroadcastUtils.kDAOCategory_RowRemove);
		for (Long uid : uids) {
			if (uid == null) {
				continue;
			}
			UserChangeNotifier.getInstance().notifyContactDelete(String.valueOf(uid));
			SyncContactHelper.deleteContactFromPhoneBook(String.valueOf(uid));
		}
	}

	private String getDeleteList(List<Long> uids) {
		StringBuilder deleteSql = new StringBuilder();
		boolean firstFlag = true;
		for (long uid : uids) {
			if (!firstFlag) {
				deleteSql.append(",");
			} else {
				firstFlag = false;
			}
			deleteSql.append("'").append(uid).append("'");
		}
		return deleteSql.toString();
	}

	@Override
	public void deleteAll() {
		IDatabaseManager mainDB = StorageSdk.getMainDB();
		if (null == mainDB) {
			return;
		}
		String selection = null;
		String[] selectionArgs =null;
		mainDB.delete(ContactsModel.class, selection, selectionArgs);
        UserBroadcastUtils.sendContactChangeIntent(UserBroadcastUtils.kDAOCategory_RowRemove);
	}

	@Override
	public void checkAllContacts(List<ContactsModel> contactList,boolean isFirstLoad) {

	}


}
